package com.find.answer.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.find.answer.domain.dto.UserDto;
import com.find.answer.domain.http.Result;
import com.find.answer.domain.po.User;
import com.find.answer.service.UserService;
import com.find.answer.utils.SMSUtils;
import com.find.answer.utils.ValidateCodeUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.soap.SAAJResult;

/**
 * @author Seon
 * @date 2022/11/1 21:11
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    private static final String USER = "user";
    private static final String SIGN_NAME = "阿里云短信测试";
    private static final String TEMPLATE_CODE = "SMS_154950909";


    @PostMapping("/loginout")
    public Result<?> logout(HttpServletRequest request) {
        HttpSession session = request.getSession();
        session.removeAttribute(USER);
        return Result.success(null);
    }

    @PostMapping("/login")
    public Result<?> login(@RequestBody UserDto userDto,
                           HttpServletRequest request) {
        String phone = userDto.getPhone();
        String code = userDto.getCode();

        HttpSession session = request.getSession();
        String initCode = (String) session.getAttribute(phone);
        if (StringUtils.isEmpty(phone) || StringUtils.isEmpty(code) || StringUtils.isEmpty(initCode)) {
            return Result.error("请先获取验证码哦");
        }


        if (initCode.equals(code)) {
            LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(User::getPhone, phone);
            User one = userService.getOne(wrapper);
            if (one == null) {
                User user = new User();
                user.setPhone(phone);
                user.setStatus(1);
                userService.save(user);
                session.setAttribute(USER, user.getId());
                return Result.success(user);
            } else {
                session.setAttribute(USER, one.getId());
                return Result.success(one);
            }
        } else {
            return Result.error("验证码错误");
        }
    }

// 19150931615  19981823539
    /**
     * 发送手机端验证那的接口。
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public Result<?> sendMessage(@RequestBody User user,
                                 HttpServletRequest request) {
        /**
         * 获取手机号
         * 生成随机的四位验证码
         * 调用阿里云提供的云服务API完成发送短信
         * 需要将生层的验证码先存起来
         * todo:现在先存到session里面，然后后期再弄到redis里面。
         */
        String phone = user.getPhone();

        if (StringUtils.isNotEmpty(phone)) {
            String code = ValidateCodeUtils.generateValidateCode(4).toString();
            log.info("{},code = {}", phone, code);
            /*
            需要的时候，再打开，不然浪费钱 呜呜呜。
             */
//            SMSUtils.sendMessage(SIGN_NAME, TEMPLATE_CODE, phone, code);
            HttpSession session = request.getSession();
            session.setAttribute(phone, code);
            return Result.success("发送成功");
        }
        return Result.error("手机号错误");
    }
}
